#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tj.db.util.cayley_util import CayleyUtil


class BaikeTriplet(object):

    def __init__(self, subject, predicate, object):
        self.subject = subject
        self.predicate = predicate
        self.object = object

    def __eq__(self, other):
        return self.subject == other.subject \
            and self.predicate == other.predicate \
            and self.object == other.object

    def __str__(self):
        return "(%s, %s, %s)" % (self.subject, self.predicate, self.object)


class CategoryTriplet(BaikeTriplet):

    def __init__(self, category_name, subcategory_name):
        super(CategoryTriplet, self).__init__(category_name, "is-a", subcategory_name)

    def __eq__(self, other):
        return self.subject == other.subject \
            and self.object == other.object


class InstanceTriplet(BaikeTriplet):

    def __init__(self, category_name, instance_name):
        super(InstanceTriplet, self).__init__(category_name, "instance-of", instance_name)

    def __eq__(self, other):
        return self.subject == other.subject \
            and self.object == other.object


class Triplets(object):

    def __init__(self):
        self.__triplets = []

    def __exists(self, other):
        for triplet in self.__triplets:
            if triplet == other:
                return True
        return False

    def append(self, triplet):
        if not self.__exists(triplet):
            self.__triplets.append(triplet)
            return True
        return False

    def display(self):
        for triplet in self.__triplets:
            print triplet

    def import_to_cayley(self):
        template = """
        {
            "subject": "%s",
            "predicate": "%s",
            "object": "%s"
        }"""

        count = 0
        import_quads = "["
        for triplet in self.__triplets:
            if count != 0:
                import_quads += ","
            import_quads += template % (triplet.subject, triplet.predicate, triplet.object)
            count += 1
        import_quads += "]"

        print import_quads
        cayley_util = CayleyUtil()
        print cayley_util.insert_quads(import_quads)


class CategoryTriplets(Triplets):
    pass


class InstanceTriplets(Triplets):
    pass













